<template>
  <a-modal
    :width="800"
    v-model:visible="modalVisible"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <template #title> 常用功能 </template>
    <div style="height: 600px; overflow: auto">
      <div v-for="item in data" :key="item.menuId">
        <a-space direction="vertical" fill>
          <div
            ><a-space direction="vertical" fill :size="20"
              ><div style="font-size: 16px">{{ item.menuName }}</div>
              <div style="display: flex">
                <a-checkbox-group v-model:modelValue="modelValue" :max="6">
                  <a-checkbox
                    v-for="element in item.children"
                    :key="element.menuId"
                    :value="element.menuId"
                  >
                    <template #checkbox="{ checked }">
                      <a-space
                        :size="20"
                        align="start"
                        class="custom-checkbox-card"
                        :class="{ 'custom-checkbox-card-checked': checked }"
                      >
                        <div className="custom-checkbox-card-mask">
                          <div className="custom-checkbox-card-mask-dot" />
                        </div>
                        <div>
                          <div className="custom-checkbox-card-title">
                            <div
                              class="itemBox"
                              :style="{
                                backgroundColor: element.backgroundColor,
                              }"
                            >
                              <i :class="getIcon(element.icon)"></i>
                            </div>
                          </div>
                          <a-typography-text type="secondary">
                            {{ element.menuName }}
                          </a-typography-text>
                        </div>
                      </a-space>
                    </template>
                  </a-checkbox></a-checkbox-group
                >
                <!-- <div
                v-for="element in item.node"
                :key="element.menuId"
                style="display: flex; margin: 20px"
              >
                <div>
                  <a-space direction="vertical" fill>
                    <div class="itemBox">
                      <i :class="getIcon(element.icon)"></i>
                    </div>
                    <div>{{ element.menuName }}</div></a-space
                  >
                </div>
              </div> -->
              </div></a-space
            >
          </div>
        </a-space>
      </div>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { reactive, computed, ref, watchEffect } from 'vue';
  import { GetRouters } from '@/api/menu';
  import { cloneDeep } from 'lodash';
  import { flatTree } from '@/utils/utils';

  const props = withDefaults(
    defineProps<{
      visible: boolean;
    }>(),
    {
      visible: false,
    }
  );

  const emit = defineEmits(['update:visible', 'selectMenu']);

  const getIcon = (icon: string) => {
    if (!icon) return;
    return `iconfont ${icon}`;
  };

  // 判断用户是否显示弹出框
  const modalVisible = computed({
    get() {
      return props.visible;
    },
    set(visible: boolean) {
      emit('update:visible', visible);
    },
  });

  function getRandomColor() {
    const letters = '0123456789ABCDEF';
    let color = '#';
    for (let i = 0; i < 6; i += 1) {
      color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
  }

  const data = ref<any>([]);
  const modelValue = ref();
  const getData = async () => {
    const res = JSON.parse(sessionStorage.getItem('SideBarRouters') as any);
    res.forEach((ele: any, index: any) => {
      if (ele.children) {
        ele.children.forEach((item: any) => {
          if (item.menuName === '基础设置') return;
          if (item.children) {
            data.value.push({ ...item });
          }
        });
      } else {
        res.splice(index, 1);
      }
    });
    data.value.forEach((item) => {
      item.children.forEach((ele) => {
        ele.backgroundColor = getRandomColor();
      });
    });
  };
  getData();
  // 取消
  const handleCancel = () => {
    modalVisible.value = false;
  };

  const handleOk = () => {
    const modelMenu: any = [];
    const newAddr = flatTree(data.value, 'children');
    modelValue.value.forEach((item) => {
      const index = newAddr.findIndex((ele) => ele.menuId === item);
      modelMenu.push({ ...newAddr[index] });
    });
    emit('selectMenu', modelMenu);
    modalVisible.value = true;
  };

  // watchEffect(() => {});
</script>

<style scoped lang="less">
  .demo-basic {
    margin-top: 5px;
    width: 500px;
    // margin: auto;
    background-color: aliceblue;
  }

  .custom-checkbox-card {
    padding: 10px 16px;
    // border: 1px solid var(--color-border-2);
    border-radius: 4px;
    width: 120px;
    height: 80px;
    box-sizing: border-box;
    font-size: 13px;
    white-space: nowrap;
    /*设置文本超出元素宽度部分隐藏*/
    overflow-x: hidden;
    /*设置文本超出部分用省略号显示*/
    text-overflow: ellipsis;
  }

  .custom-checkbox-card-mask {
    height: 14px;
    width: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 2px;
    border: 1px solid var(--color-border-2);
    box-sizing: border-box;
  }

  .custom-checkbox-card-mask-dot {
    width: 8px;
    height: 8px;
    border-radius: 2px;
  }

  .custom-checkbox-card-title {
    color: var(--color-text-1);
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 8px;
  }

  .custom-checkbox-card:hover,
  .custom-checkbox-card-checked,
  .custom-checkbox-card:hover .custom-checkbox-card-mask,
  .custom-checkbox-card-checked .custom-checkbox-card-mask {
    border-color: rgb(var(--primary-6));
  }

  .custom-checkbox-card-checked {
    background-color: var(--color-primary-light-1);
  }

  .custom-checkbox-card:hover .custom-checkbox-card-title,
  .custom-checkbox-card-checked .custom-checkbox-card-title {
    color: rgb(var(--primary-6));
  }

  .custom-checkbox-card-checked .custom-checkbox-card-mask-dot {
    background-color: rgb(var(--primary-6));
  }

  .itemBox {
    width: 30px;
    height: 30px;

    border: none;
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
